﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pub.Lib;
using Pub.Tools;
using Pub;

namespace MyAPP
{
    public partial class FrmLogin : Pub.Controls.MastForm.BaseDlg
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private Boolean bSavePass = false;


        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.Show();

            //读取帐套
            txtAccount.Text = XmlTools.Read(PubVar.UserXml, "UserSet/User/Account");

            //读取用户名
            txtUser.Text = XmlTools.Read(PubVar.UserXml, "UserSet/User/UserCode");

            //读取密码
            bSavePass = SysTools.ToBoolean(XmlTools.Read(PubVar.UserXml, "UserSet/User/SavePass", "False"));

            if (bSavePass)
            {
                txtPass.Text = DESEncrypt.Decrypt(DESEncrypt.Decrypt(XmlTools.Read(PubVar.UserXml, "UserSet/User/UserPass")));
            }
            if (txtUser.Text == string.Empty)
            {
                //获取当前用户名
                txtUser.Text = System.Environment.UserName;
                txtPass.Focus();
            }
            if (txtAccount.Text == string.Empty)
            {
                txtAccount.Focus();
            }
        }





        private void btnOK_Click(object sender, EventArgs e)
        {
            //检查输入是否合法
            if (!checkInput()) { return; }

            //0:用户名,1;密码,2:GUID
            string strUserGuid = SysTools.getGUID();
            string[] strArray = { txtUser.Text, txtPass.Text, strUserGuid };
            PubVar.SysArrAuth = string.Join(PubVar.SysSplit, strArray, 0, strArray.Length);

            string strMess = string.Empty;
            if (Helper.ADHelper.AuthUser(PubVar.SysArrAuth, ref strMess))
            {
                //权限检查
                string strSQL = string.Format("select * from sysUser where cUserCode='{0}'", txtUser.Text);
                DataTable dt = Helper.DBHelper.GetDataTable(strSQL);
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        List<PubVar.sysUser> Users = DBClass.DataTableToT<PubVar.sysUser>(dt);
                        PubVar.NowUser = Users[0];
                    }
                    catch (Exception ex)
                    {
                        MsgTools.ErrorMsg(ex.Message.ToString());
                        return;
                    }
                    string strRead = dt.Rows[0]["cRead"].ToString().ToUpper();
                    string strWrite = dt.Rows[0]["cWrite"].ToString().ToUpper();
                    PubVar.NowUser.bRead = strRead.IndexOf(txtAccount.Text) > -1;
                    PubVar.NowUser.bWrite = strWrite.IndexOf(txtAccount.Text) > -1;
                    if (!PubVar.NowUser.bRead)
                    {
                        MsgTools.ErrorMsg(string.Format("登录失败,用户{0}无权限访问{1}帐套.", txtUser.Text, txtAccount.Text));
                        return;
                    }
                }
                else
                {
                    MsgTools.ErrorMsg(string.Format("登录失败,用户资料表sysUser缺失.", txtUser.Text));
                    return;
                }

                //读取帐套信息
                strSQL = string.Format("select * from Corp where cCorpNo='{0}'", txtAccount.Text);
                dt = Helper.DBHelper.GetDataTable(strSQL);
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        List<PubVar.Corp> Corps = DBClass.DataTableToT<PubVar.Corp>(dt);
                        PubVar.NowCorp = Corps[0];
                    }
                    catch (Exception ex)
                    {
                        MsgTools.ErrorMsg(ex.Message.ToString());
                        return;
                    }
                }
                else
                {
                    MsgTools.ErrorMsg(string.Format("登录失败,帐套资料表Corp缺失.", txtAccount.Text));
                    return;
                }


                //保存帐套
                XmlTools.Save(PubVar.UserXml, "UserSet/User/Account",txtAccount.Text);

                //保存用户名
                XmlTools.Save(PubVar.UserXml, "UserSet/User/UserCode", PubVar.NowUser.cUserCode);

                //保存密码
                string strPass = string.Empty;
                if (bSavePass)
                {
                    strPass = DESEncrypt.Encrypt(DESEncrypt.Encrypt(txtPass.Text));
                    XmlTools.Save(PubVar.UserXml, "UserSet/User/SavePass", "True");
                }
                else
                {
                    XmlTools.Save(PubVar.UserXml, "UserSet/User/SavePass", "False");
                }
                XmlTools.Save(PubVar.UserXml, "UserSet/User/UserPass", strPass);

                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MsgTools.ErrorMsg(strMess);
                txtPass.Focus();
            }
        }

        /// <summary>
        /// 检查输入是否合法
        /// </summary>
        /// <returns></returns>
        private bool checkInput()
        {
            if (this.txtAccount.Text.Trim() == "")
            {
                MsgTools.InfoMsg("请输入帐套.");
                txtAccount.Focus();
                return false;
            }
            if (this.txtUser.Text.Trim() == "")
            {
                MsgTools.InfoMsg("请输入用户名.");
                txtUser.Focus();
                return false;
            }
            if (this.txtPass.Text == "")
            {
                MsgTools.InfoMsg("请输入密码.");
                txtPass.Focus();
                return false;

            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }




    }

}
